/**
 * Apr 26, 2010
 */
package org.tomale.id.projects.ui;

import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.editor.FormEditor;
import org.eclipse.ui.forms.editor.FormPage;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.forms.widgets.TableWrapData;
import org.eclipse.ui.forms.widgets.TableWrapLayout;

/**
 * @author ferd
 *
 */
public class PredefinedTaskMaterials extends FormPage {

	public static final String PAGE_ID = "org.tomale.id.projects.ui.editor.predefinedtasks.materials";
	public static final String PAGE_TITLE = "Materials";

	PredefinedTaskEditorInput _input;
	TableViewer _viewer;

	public PredefinedTaskMaterials(FormEditor editor){
		super(editor,PAGE_ID,PAGE_TITLE);
	}

	@Override
	protected void createFormContent(IManagedForm managedForm) {
		_input = (PredefinedTaskEditorInput) getEditorInput();
		
		final ScrolledForm form = managedForm.getForm();
		FormToolkit tk = managedForm.getToolkit();
		Composite body = form.getBody();
		tk.paintBordersFor(body);
		form.setText(this.getTitle());
		
		TableWrapLayout layout = new TableWrapLayout();
		layout.numColumns = 2;
		body.setLayout(layout);
		
		createMaterialsSection(body, tk);
	}
	
	private void createMaterialsSection(Composite parent, FormToolkit toolkit){
		Section section = toolkit.createSection(parent,
				Section.DESCRIPTION | Section.TITLE_BAR | Section.EXPANDED);
		section.setText("Materials");
		section.setDescription("This section describes the materials to be used in this task.");
		TableWrapData td = new TableWrapData(TableWrapData.FILL);
		section.setLayoutData(td);
		
		Composite comp = toolkit.createComposite(section);
		toolkit.paintBordersFor(comp);
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		comp.setLayout(layout);

		_viewer = new TableViewer(comp);
		_viewer.setContentProvider(new ContentProvider());
		_viewer.setLabelProvider(new ViewLabelProvider());
		toolkit.adapt(_viewer.getTable());
		initTable(_viewer.getTable());
		
		Button add = toolkit.createButton(comp, "Add", SWT.PUSH);
		add.setLayoutData(new GridData(SWT.FILL,SWT.FILL,false,false));
		
		Button remove = toolkit.createButton(comp, "Remove", SWT.PUSH);
		remove.setLayoutData(new GridData(SWT.FILL,SWT.FILL,false,false));
		
		section.setClient(comp);
	}
	
	private void initTable(Table table){
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		table.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true,1,2));
		
		TableColumn col;
		
		// name
		col = new TableColumn(table, SWT.NONE);
		col.setText("Name");
		col.setWidth(100);
		col.setResizable(true);
		col.setMoveable(true);
		
		// quantity
		col = new TableColumn(table, SWT.NONE);
		col.setText("Quantity");
		col.setWidth(100);
		col.setResizable(true);
		col.setMoveable(true);
		
		// unit
		col = new TableColumn(table, SWT.NONE);
		col.setText("Unit");
		col.setWidth(100);
		col.setResizable(true);
		col.setMoveable(true);
	}
	
	class ContentProvider implements IStructuredContentProvider {

		@Override
		public Object[] getElements(Object inputElement) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public void dispose() {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
			// TODO Auto-generated method stub
			
		}
		
	}
	
	class ViewLabelProvider extends LabelProvider implements ITableLabelProvider {

		@Override
		public Image getColumnImage(Object element, int columnIndex) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public String getColumnText(Object element, int columnIndex) {
			// TODO Auto-generated method stub
			return null;
		}
		
	}
}
